def Welcome():
    print("==============欢迎使用计算器V1.0.2==============")
    print("1.加法")
    print("2.减法")
    print("3.乘法")
    print("4.除法")
    print("5.取余")
    print("6.取整")
    print("7.幂运算")
    print("8.历史记录")
    choice = input("请选择：")
    return choice


def check_input_int(input_str):
    running = True
    while running:
        try:
            input_str = float(input_str)  # 支持浮点数输入
            running = False
            return input_str
        except ValueError:
            input_str = input("输入错误，请重新输入：")


def check_input_choice(input_str):
    running = True
    while running:
        try:
            input_str = int(input_str)
            if 1 <= input_str <= 8:
                running = False
                return input_str
            else:
                # 修复：提示范围从1-7改为1-8，与实际选项一致
                input_str = input("选择错误，请输入1-8之间的数字：")
        except ValueError:
            input_str = input("输入错误，请重新输入数字：")


class Operations:
    # 加法
    def add(self, num1, num2):
        print(f"{num1} + {num2} = {num1 + num2}")
        with open("history.txt", "a", encoding="utf-8") as f:
            f.write(f"{num1} + {num2} = {num1 + num2}\n")

    # 减法
    def subtract(self, num1, num2):
        print(f"{num1} - {num2} = {num1 - num2}")
        with open("history.txt", "a", encoding="utf-8") as f:
            f.write(f"{num1} - {num2} = {num1 - num2}\n")

    # 乘法
    def multiply(self, num1, num2):
        print(f"{num1} * {num2} = {num1 * num2}")
        with open("history.txt", "a", encoding="utf-8") as f:
            f.write(f"{num1} * {num2} = {num1 * num2}\n")

    # 除法
    def divide(self, num1, num2):
        try:
            result = num1 / num2
            print(f"{num1} / {num2} = {result}")
            with open("history.txt", "a", encoding="utf-8") as f:
                f.write(f"{num1} / {num2} = {result}\n")
        except ZeroDivisionError:
            print("错误：除数不能为零！")

    # 取余
    def modulus(self, num1, num2):
        try:
            result = num1 % num2
            print(f"{num1} % {num2} = {result}")
            with open("history.txt", "a", encoding="utf-8") as f:
                f.write(f"{num1} % {num2} = {result}\n")
        except ZeroDivisionError:
            print("错误：除数不能为零！")

    # 取整除法
    def floor_divide(self, num1, num2):
        try:
            result = num1 // num2
            print(f"{num1} // {num2} = {result}")
            with open("history.txt", "a", encoding="utf-8") as f:
                f.write(f"{num1} // {num2} = {result}\n")
        except ZeroDivisionError:
            print("错误：除数不能为零！")

    # 幂运算
    def power(self, num1, num2):
        # 修复：仅禁止0^0（无意义），允许其他幂运算（如2^0=1、0^3=0）
        if num1 == 0 and num2 == 0:
            print("错误：0的0次幂无意义！")
        else:
            result = num1 **num2
            print(f"{num1} ^ {num2} = {result}")
            with open("history.txt", "a", encoding="utf-8") as f:
                f.write(f"{num1} ^ {num2} = {result}\n")


def read_history():
    try:
        with open("history.txt", "r", encoding="utf-8") as f:
            read_ = f.read()
            print(read_)
    except FileNotFoundError:
        print("没有历史记录哦，快去做计算吧~")